import type { taskType } from "@/types/utils.ts";


/**
 * 简单的提示消息
 * @param title 消息内容
 * @param type 消息类型
 * @param mask 是否显示透明蒙层，防止触摸穿透
 * @param duration 是否显示透明蒙层，防止触摸穿透，默认：false
 */
export const useTask = (title: string, type: taskType = "none", mask: boolean = true, duration: number = 1000) => {
    let iconPath: string = "";
    if (["error", "warn", "hint"].includes(type)) {
        duration = 3500;
    }
    if (title.length > 7) {
        type = "none";
    }
    if (type !== "none") {
        iconPath = "/static/" + type + ".png";
    }
    uni.showToast({ title: title, icon: "none", image: iconPath, mask: mask, duration });
}


/**
 * 确定模态框
 * @param title 模态框标题
 * @param content 模态框内容
 * @param confirmText 确认按钮文字
 * @param cancelText 取消按钮文字
 * @returns
 */
export const useModal = (title?: string, content?: string, confirmText?: string, cancelText?: string) => {
    return new Promise((resolve, reject) => {
        uni.showModal({
            title: title,
            content: content,
            showCancel: true,
            confirmText: confirmText,
            cancelText: cancelText,
            success: ({ confirm, cancel }) => {
                if (confirm) {
                    return resolve(confirm);
                }
                if (cancel) {
                    return reject(confirm);
                }
            },
            fail: () => {
                return reject("error");
            },
        });
    });
};

/**
 * 警告
 * @param content 内容
 * @param confirmText 确认按钮文字
 * @returns
 */
export const useAlert = (content: string, confirmText: string = "关闭") => {
    return new Promise(() => {
        uni.showModal({
            content: content,
            showCancel: false,
            confirmText: confirmText,
        });
    });
};


